package com.dragon8869.myspringboot.controller;

import com.dragon8869.myspringboot.annotation.LogExecuteTime;
import com.dragon8869.myspringboot.exception.BusinessException;
import com.dragon8869.myspringboot.entity.AyUser;
import com.dragon8869.myspringboot.service.AyUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("ayUser")
@Log4j2
public class AyUserController {

    @Resource
    AyUserService ayUserService;

    @LogExecuteTime
    @RequestMapping("list")
    public String list(Model model) {
        List<AyUser> all = ayUserService.findAll();
        model.addAttribute("users", all);
        return "ayUserList";
    }

    @LogExecuteTime
    @RequestMapping("list/{pageId},{pageSize}")
    public String list(Model model, @PathVariable int pageId, @PathVariable int pageSize) {
        Page<AyUser> all = ayUserService.findAll(PageRequest.of(pageId, pageSize));
        model.addAttribute("users", all.getContent());
        return "ayUserList";
    }

    @RequestMapping("{id}")
    public String list(Model model, @PathVariable Integer id) {
        AyUser byId = ayUserService.findById(id);
        model.addAttribute("user", byId);
        return "ayUserDetail";
    }

    @RequestMapping("create")
    public String create(Model model, AyUser ayUser) {
        model.addAttribute("user", ayUser);
        return "ayUserEdit";
    }

    @RequestMapping("edit")
    public String edit(Model model, AyUser ayUser) {
        model.addAttribute("user", ayUser);
        return "ayUserEdit";
    }

    @PostMapping("save")
    @ResponseBody
    public AyUser save(AyUser ayUser) {
        return ayUserService.save(ayUser);
    }

    @GetMapping
    public String ayUser(Model model) {
        List<AyUser> all = ayUserService.findAll();
        model.addAttribute("users", all);
        throw new BusinessException("业务异常");
    }

    @GetMapping(value = "{id}/downInfo", produces = "application/json;charset=utf-8")
    public void downAyUserInfo(HttpServletResponse response, @PathVariable Integer id) throws IOException {
        AyUser ayUser = ayUserService.findById(id);
//        response.addHeader("Content-Disposition", "inline;filename=" + ayUser.getName() + ".json");
//        response.addHeader("Content-Disposition", "attachment;filename=" + ayUser.getName() + ".json");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getOutputStream(), ayUser);
    }

    @GetMapping(value = "{id}/downInfo/{downloadType}", produces = "application/json;charset=utf-8")
    public void downAyUserInfo(HttpServletResponse response, @PathVariable Integer id, @PathVariable String downloadType) throws IOException {
        AyUser ayUser = ayUserService.findById(id);
//        response.addHeader("Content-Disposition", "inline;filename=" + ayUser.getName() + ".json");
//        response.addHeader("Content-Disposition", "attachment;filename=" + ayUser.getName() + ".json");
        response.addHeader("Content-Disposition", downloadType + ";filename=" + URLEncoder.encode(ayUser.getName(), StandardCharsets.UTF_8) + ".json");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getOutputStream(), ayUser);
        log.info(mapper.writeValueAsString(ayUser));
    }

    @Resource
    private JdbcTemplate jdbcTemplate;

    @ResponseBody
    @RequestMapping("/user_list")
    public List<Map<String, Object>> ayUserList(){
        String sql = "select id, name, password from ay_user";
        return jdbcTemplate.queryForList(sql);
    }

}
